<template>
  <div class="companypublish">
    <el-row>
      <el-col :span="4" :offset="3">
        <div class="div_border_title">
          <h2>企业发布</h2>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="18" :offset="3" class="background">
        <div class="search-box">
          <el-form inline>
            <el-form-item label="职位">
              <el-select v-model="searchDate.job_title" @change="searchChange" filterable>
                <el-option v-for="(job,index) in job_data" :key="index" :label="job" :value="job"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="doSearch" icon="search">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col :span="18" :offset="3" class="tabledata">
        <div class="grid-content bg-purple-light1">
          <div class="toolbar-box">
            <el-button-group>
              <el-button v-for="(toolbar,index) in toolbarList" :key="index" :type="toolbar.type" :icon="toolbar.icon"
                         @click="getButtonType(toolbar.event)">{{toolbar.name}}
              </el-button>
            </el-button-group>
          </div>
          <el-table :data="publishData" border tooltip-effect="dark" style="width: 100%"
                    @row-dblclick="findMorePublishInfo" @row-click="clickRow">
            <el-table-column type="index" label="序号" width="80" align='center'></el-table-column>
            <el-table-column prop="company_name" label="公司" align='center'>
            </el-table-column>
            <el-table-column prop="job_title" label="职位" align='center' show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="work_space" label="工作地点" align='center' show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="salary" label="薪资待遇(元)" align='center' show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="release_date" label="过期时间" align='center' show-overflow-tooltip>
            </el-table-column>
          </el-table>
          <el-pagination @current-change="handleCurrentChange" layout="total, prev, pager, next" :total="total"
                         v-if="total>5"></el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import { postDataToServer } from '../../assets/js/post.js'
  import { commonNotify, changeSalaryStyle } from '../../assets/js/common.js'
  import { getToolBarList } from '../../assets/js/buildStructure/list.js'

  export default {
    name: 'companypublish',
    data () {
      return {
        total: 0,
        page_no: 1,
        currentId: '',
        publishData: [],
        jobList: [],
        job_data: [],
        toolbarList: [],
        searchDate: {
          job_title: '',
        },
      }
    },
    created () {
      this.getToolBars()
      this.getPublishData(1)
    },
    methods: {
      searchChange () {
        this.doSearch()
      },
      handleCurrentChange (pageNum) {
        this.page_no = pageNum
      },
      clickRow (row, event, column) {
        this.currentId = row.id
      },
      getToolBars () {
        this.toolbarList = getToolBarList('list', 7)
      },
      doSearch () {
        let _this = this
        let params = new Object()
        params.job_names = _this.searchDate.job_title
        postDataToServer('HM_COMPANY_JOB_LISTALLBYNAME', params).then(function (data) {
          if (data.status === 0) {
            _this.publishData = data.data_list
            _this.publishData.forEach((e) => {
              e = changeSalaryStyle(e)
            })
          }
        })
      },
      //获取数据
      getPublishData (pageNum) {
        let _this = this
        let data = new Object()
        data.page_no = pageNum
        data.page_size = 10
        data.company_id = 'F1DEF6A550904EC9B956E1B6221A0204'
        let s
        for (s in _this.searchDate) {
          if (_this.searchDate[s] !== '') {
            data[s] = _this.searchDate[s]
          }
        }
        postDataToServer('HM_COMPANY_JOB_LISTPAGE', data).then(function (data) {
          _this.total = data.total_results
          _this.publishData = data.data_list
          for (let i = 0; i < _this.publishData.length; i++) {
            _this.jobList[i] = _this.publishData[i].job_title
          }
          let arr = new Set(_this.jobList)
          _this.job_data = Array.from(arr)
          _this.publishData.forEach((e) => {
            e = changeSalaryStyle(e)
          })
        })
      },
      getButtonType (type) {
        let _this = this
        switch (type) {
          case 'add':
            _this.$router.push({'path': '/companypublishdetial/newpublish'})
            break
          case 'edit':
            _this.edit()
            break
          case 'delete':
            _this.delete()
            break
          default:
            // statements_def
            break
        }
      },
      //编辑
      edit () {
        if (this.currentId !== '') {
          this.$router.push({'path': '/companypublishdetial/' + this.currentId})
          this.currentId = ''
        } else {
          this.$message({
            message: '请选中一行',
            type: 'warning'
          })
        }
      },
      //删除
      deleteOne () {
        let _this = this
        let pargram = new Object()
        pargram.id = this.currentId
        postDataToServer('HM_COMPANY_JOB_DEL', pargram).then((data) => {
          if (data.status === 0) {
            this.$message({
              message: '删除成功！',
              type: 'success'
            })
            this.getPublishData(1)
          } else {
            this.$message({
              message: data.message,
              type: 'warning'
            })
          }
        })
      },
      delete () {
        if (this.currentId !== '') {
          let _this = this
          this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.deleteOne()
          }).catch(() => {
            _this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        } else {
          this.$message({
            message: '请选中一行',
            type: 'warning'
          })
        }
      },
      findMorePublishInfo (row, event) {
        let id = row.id
        let _path = '/companypublishdetial/' + id
        this.$router.push({
          path: _path
        })
      }
    },
    watch: {
      page_no (val, oldVal) {
        this.getPublishData(val)
      }
    }
  }
</script>
<style scoped>
  .companypublish {
    padding-top: 2em;
  }

  .background {
    margin-top: 0.9em;
  }

  .title {
    border-bottom: 1px solid #F2F2F2;
    padding: 0.5em;
    color: #575B6A;
    font-size: 1.3em;
  }

  .tabledata {
    color: #575B6A;
    min-height: 25em;
  }

  .search-box {
    padding: 0em 1em;
  }

  .toolbar-box {
    border: 1px solid #20A0FF;
    background-color: #20A0FF;
  }

  .el-pagination {
    margin: 0.5em;
  }
</style>
